import json
import os
import requests

import util

url = os.getenv('JIANMU_BOOT_WEBHOOK_URL')
secret = os.getenv('JIANMU_SECRET')
signs = util.get_signature(secret)
title = os.getenv('JIANMU_MSG_TITLE')
text = os.getenv('JIANMU_MSG_TEXT')
pic_url = os.getenv('JIANMU_PIC_URL')
message_url = os.getenv('JIANMU_MSG_URL')

headers = {
    'Content-Type': 'application/json'
}
params = {
    'sign': signs.get('sign'),
    'timestamp': signs.get('timestamp'),
}
data = {
    'msgtype': 'link',
    'link': {
        'title': title,
        'text': text,
        'picUrl': pic_url,
        'messageUrl': message_url
    }
}
response = requests.post(url=url, headers=headers, params=params, data=json.dumps(data))
content = response.json()
if content.get("errcode") != 0:
    raise Exception(content)
print(content)
